<?php
	include "layout.php";
	
	head("Planets");
?>
<h1>List of Planets</h1>
<h2>Ordered By Name</h2>
<?php
	if (!isset($_GET["page"])) {
		$page = 1;
	}
	else {
		$page = $_GET["page"];
	}
	$lower = ($page - 1)*15;
	$upper = $page*15;
	$next = $page + 1;
	$prev = $page - 1;
	echo "<table border=1>";
	echo "<tr>";
	echo "<th>Name</th>";
	echo "<th>Size</th>";
	echo "<th>Deposits Found</th>";
	echo "<th></th>";
	echo "</tr>";
	$query = mysql_query("SELECT * FROM planets ORDER BY name LIMIT {$lower},{$upper}");
	$entries = mysql_num_rows($query);
	while ($results = mysql_fetch_array($query)) {
		$deposits = mysql_query("SELECT * FROM deposits WHERE planet={$results["id"]}");
		$count = mysql_num_rows($deposits);
		echo "<tr>";
		echo "<td style='text-align:left'><a href='planet.php?id={$results["id"]}'>{$results["name"]}</a></td>";
		echo "<td>{$results["size"]}x{$results["size"]}</td>";
		echo "<td>{$count}</td>";
		echo "<td><a href='editplanet.php?id={$results["id"]}'>Edit?</a></td>";
		echo "</tr>";
	}
	echo "<tr>";
	echo "<td colspan=4><a href='addplanet.php'>Create a New Planet</a></td>";
	echo "</tr>";
	if ($page > 1 || $entries == 15) {
		echo "<tr>";
		if ($page > 1) {
			echo "<td colspan=2><a href='planets.php?page={$prev}'>Previous Page</a></td>";
		}
		else {
			echo "<td colspan=2></td>";
		}
		if ($entries < 15) {
			echo"<td colspan=2></td>";
		}
		else {
			echo"<td colspan=2><a href='planets.php?page={$next}'>Next Page</a></td>";
		}
		echo "</tr>";
	}
	echo "</table>";
?>
<?php
	foot();
?>